//引入创建Slice
import {
    createSlice,       //创建Slice
    createAsyncThunk   //创建异步action
} from '@reduxjs/toolkit'

import {
    getMovieRequest
} from '../../api/index'


//我们使用createAsyncThunk方法来创建异步action
//createAsyncThunk接受两个参数,第一个参数是type的值,第二个参数是回调函数,回调函数就是异步action的函数
export const getMovieAction = createAsyncThunk("movie/getMovieList" , async () => {
    //在当前函数中执行异步操作
    const re = await getMovieRequest()
    console.log(re , "re");
    return re.data.data.list
    //在这个函数中return的值，就是在thunk中间件中再次disptch的值
})



//创建Slice
const movieSlice = createSlice({
    name: "movie",

    initialState: {
        movieList: []
    },

    reducers: {

    },

    //异步action的处理,需要书写额外的reducer处理,不能写在reduers中,因为reducers会创建一个对应的action
    extraReducers: {
        [getMovieAction.fulfilled](prev, action) {
            console.log("异步action成功--getMovieAction");
            console.log(prev, action);
            prev.movieList = action.payload
        },
        [getMovieAction.rejected]() {
            console.log("异步action失败--getMovieAction");
        },
        [getMovieAction.pending]() {
            console.log("异步action正在进行中--getMovieAction");
        },
    },

})

export default movieSlice.reducer;

